; Brother OP2 cpmish BIOS © 2020 David Given
; This file is distributable under the terms of the 2-clause BSD license.
; See COPYING.cpmish in the distribution root directory for more information.

    maclib cpm
    maclib cpmish
    maclib brotherop2

    extrn SYSIN
    extrn SYSOUT
    extrn ADDAHL

    public TTYINIT
    public TTYPUTC
    public TTYPUT8
    public TTYPUT16
    public TTYPUTSI
    public CURSTOGG

    cseg

CURSOR_UPDATES = 0
CLEAR_SCREEN_ON_INIT = 1
EMULATE_CLEAR_TO_EOL = 1
EMULATE_CLEAR_TO_EOS = 1
    maclib tty
    maclib print

TTYINIT equ tty_init
TTYPUTC equ tty_putc
TTYPUT8 equ tty_puthex8
TTYPUT16 equ tty_puthex16
TTYPUTSI equ tty_putsi

tty_rawwrite:
    ld c, a
    call goto_cursor
    xor a
    out (PORT_VIDEO_DATA), a
    ld a, 0x7f
    and c
    out (PORT_VIDEO_DATA), a
    ret

goto_cursor:
    ld a, (tty_cursorx)
    add a
    out (PORT_VIDEO_LO), a
    ld a, (tty_cursory)
    out (PORT_VIDEO_HI), a
    ret
    
; Attribute bits:
;   0 = underline
;   1 = overline
;   2 = font 2
;   3 = right-hand line?
;   4 = inverse
;   5 = inverse blink top half
;   6 = inverse blink bottom half
;   7 = blink text

CURSTOGG:
    push af
    push bc
    call goto_cursor
    nop
    nop
    in a, (PORT_VIDEO_DATA)
    xor 0x60
    ld c, a
    call goto_cursor
    nop
    out0 (PORT_VIDEO_DATA), c
    pop bc
    pop af
    ret
    
tty_delete_line
    ld a, (tty_cursory)     ; dest line
    cp SCREEN_HEIGHT - 1
    jr z, blank_bottom_line ; if already on the last line, don't move anything
    ld h, a
.1
    ld l, 0                 ; char
.2
    inc h                   ; switch to source line
    out0 (PORT_VIDEO_LO), l
    out0 (PORT_VIDEO_HI), h
    dec h                   ; move back to dest
    nop
    nop
    in a, (PORT_VIDEO_DATA)
    out0 (PORT_VIDEO_LO), l
    out0 (PORT_VIDEO_HI), h
    inc l                   ; next char
    out (PORT_VIDEO_DATA), a
    ld a, 160
    cp l
    jr nz, .2

    inc h
    ld a, SCREEN_HEIGHT - 1
    cp h
    jr nz, .1

    ; Blank the bottom line of the screen.


blank_bottom_line:
    ld a, SCREEN_HEIGHT - 1
blank_line_a:
    ld b, 0
    out0 (PORT_VIDEO_LO), b
    out (PORT_VIDEO_HI), a
    ld b, 160
    xor a
.1
    out (PORT_VIDEO_DATA), a
    nop
    nop
    djnz .1
    ret

tty_insert_line:
    ld a, (tty_cursory)     ; src line
    cp SCREEN_HEIGHT - 1
    jr z, blank_current_line; if already on the last line, don't move anything
    ld h, a
.1
    ld l, 0                 ; char
.2
    out0 (PORT_VIDEO_LO), l
    out0 (PORT_VIDEO_HI), h
    inc h                   ; move to dest line
    nop
    nop
    in a, (PORT_VIDEO_DATA)
    out0 (PORT_VIDEO_LO), l
    out0 (PORT_VIDEO_HI), h
    dec h                   ; move back to src line
    out (PORT_VIDEO_DATA), a
    inc l                   ; next char
    ld a, 160
    cp l
    jr nz, .2

    inc h
    ld a, SCREEN_HEIGHT - 1
    cp h
    jr nz, .1

    ; Blank the current line.
blank_current_line:
    ld a, (tty_cursory)
    jr blank_line_a

; vim: ts=4 sw=4 et ft=asm

